package com.huang.springbootthymeleafshiro.config;



import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.huang.springbootthymeleafshiro.shiro.realm.CustomerRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class shiroconfig {


    //shiro 和  thymeleaf 整合后  页面解析方言
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }

 //创建shiroFilter
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager manager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
            factoryBean.setSecurityManager(manager);
            //设置系统首先资源
        Map<String, String> map = new HashMap<>();
        map.put("/login.html","anon");//设置公共资源
        map.put("/register.html","anon");
        map.put("/user/register","anon");
        map.put("/**","authc"); //受限资源
        //有个默认的认证资源 没认证默认跳转
        factoryBean.setLoginUrl("/user/loginview");

        factoryBean.setFilterChainDefinitionMap(map);
        return factoryBean;
    }
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(Realm realm){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
            manager.setRealm(realm);
        return manager;
    }
    @Bean
    public Realm realm(){
        CustomerRealm realm = new CustomerRealm();

        return realm;
    }

}
